package com.wetool.wechat.model.official;

import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;

@Data
public class ServerAuthRequest {
    /**
     * 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     */
    private String signature;
    /**
     * 时间戳
     */
    private String timestamp;
    /**
     * 随机数
     */
    private String nonce;

    /**
     * 随机字符串
     */
    private String echostr;


    public String createSignature(String token) {

        String[] strings = new String[]{this.nonce, this.timestamp, token};

        Arrays.sort(strings);
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : strings) {
            stringBuilder.append(s);
        }

        String sign = DigestUtils.sha1Hex(stringBuilder.toString());
        return sign;
    }
}
